package com.bc.bcapi.service;

import com.bc.bcapi.dao.T_Common_report_subMapper;
import com.bc.bcmodel.domain.T_Common_report_subModel;
import com.bc.bcmodel.req.CommonReportDetailReq;
import com.bc.bcmodel.resp.AdminReportListResp;
import com.bc.bcmodel.resp.T_Common_reportModelResp;
import com.bc.commoncore.util.PageInfoExt;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_Common_reportMapper;
import com.bc.bcmodel.domain.T_Common_reportModel;

/**
 * @Name T_Common_reportService * @Description  * @Author generation * @Date 2019/4/18 9:28:01 * @Version 1.0
 **/
@Service
public class T_Common_reportService {
    @Resource
    T_Common_reportMapper t_Common_reportMapper;
    @Resource
    T_Common_report_subService t_common_report_subService;
    @Resource
    T_Common_report_subMapper t_common_report_subMapper;

    /**
     * 保存单条数据     *      * @return
     */
    public int insert(T_Common_reportModel req) {
        return t_Common_reportMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */
    public int insertBatch(List<T_Common_reportModel> req) {
        return t_Common_reportMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    public T_Common_reportModel findByNamedParam(T_Common_reportModel req) {
        return t_Common_reportMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    public List<T_Common_reportModel> getAllList(T_Common_reportModel req) {
        return t_Common_reportMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    public PageInfo<T_Common_reportModel> getList(T_Common_reportModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Common_reportModel> list = t_Common_reportMapper.findByNamedParamList(req);
        return new PageInfo<T_Common_reportModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */
    public int update(T_Common_reportModel req) {
        return t_Common_reportMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */
    public int delete(BigInteger id) {
        return t_Common_reportMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    public long count(T_Common_reportModel req) {
        return t_Common_reportMapper.count(req);
    }

    public int insertReport(T_Common_reportModel req) {

        T_Common_reportModel t_common_reportModel = new T_Common_reportModel();
        t_common_reportModel.setIco_name(req.getIco_name());
        List<T_Common_reportModel> list = t_Common_reportMapper.findByNamedParamList(t_common_reportModel);
        BigInteger rid = null;
        if (list == null || list.size() <= 0) {
//添加
            t_Common_reportMapper.insert(req);
            rid = req.getId();
        } else {
            //追加
            rid = list.get(0).getId();
        }
        T_Common_report_subModel t_common_report_subModel = new T_Common_report_subModel();
        t_common_report_subModel.setRid(rid);
        t_common_report_subModel.setContent(req.getContent());
        t_common_report_subModel.setEmail(req.getEmail());
        t_common_report_subModel.setImages(req.getImages());
        t_common_report_subModel.setUid(req.getUser_id());
        t_common_report_subService.insert(t_common_report_subModel);
        return 1;
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    public PageInfo<T_Common_reportModelResp> getReportList(T_Common_reportModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Common_reportModelResp> list = t_Common_reportMapper.getReportList(req);

        if (list == null || list.size() <= 0) {
            return null;
        }
        PageInfo<T_Common_reportModelResp> pageInfo = new PageInfo<T_Common_reportModelResp>(list);

        for (T_Common_reportModelResp item :
                pageInfo.getList()) {
            PageHelper.startPage(1, 3);
            T_Common_report_subModel t_common_report_subMode = new T_Common_report_subModel();
            t_common_report_subMode.setRid(item.getId());
            t_common_report_subMode.setAudit_status(1);
            PageInfo<T_Common_report_subModel> subList = t_common_report_subService.getList(t_common_report_subMode);
            if (subList != null && subList.getList() != null) {
                item.setList(subList.getList());
            }
        }
        return pageInfo;
    }

    public PageInfo<T_Common_report_subModel> getReportDetail(CommonReportDetailReq req) {
//        T_Common_reportModel t_common_reportModel = new T_Common_reportModel();
//        t_common_reportModel.setAudit_status(1);
//        t_common_reportModel.setId(req.getRid());
//
//        List<T_Common_reportModelResp> list = t_Common_reportMapper.getReportList(t_common_reportModel);
//        if (list == null || list.size() <= 0) {
//            return null;
//        }
        T_Common_report_subModel t_common_report_subModel = new T_Common_report_subModel();
        t_common_report_subModel.setRid(req.getRid());
        t_common_report_subModel.setAudit_status(1);
        PageInfo<T_Common_report_subModel> listSub = t_common_report_subService.getList(t_common_report_subModel);
        listSub.getList().forEach(t -> {
            String email = t.getEmail();
            if (!email.contains("@")) {
                email = "***";
            } else {
                String[] arr = email.split("@");
                email = email.substring(0, (arr.length / 2)) + "**@" + arr[1];
            }
            t.setEmail(email);
        });
//        T_Common_reportModelResp resp = list.get(0);
//        resp.setList(listSub);
//        return resp;
        return listSub;
    }

    public PageInfo<AdminReportListResp> getAdminList(T_Common_reportModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<AdminReportListResp> list = t_common_report_subMapper.getAdminList(req);
        return new PageInfo<AdminReportListResp>(list);
    }

    public AdminReportListResp getAdminDetail(BigInteger id) {
        AdminReportListResp adminReportListResp = t_common_report_subMapper.getAdminDetail(id);
        return adminReportListResp;
    }
}